<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="所属会员" prop="memberId">
        <el-input
            v-model="queryParams.memberId"
            placeholder="请输入所属会员"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型ID" prop="typeId">
        <el-input
            v-model="queryParams.typeId"
            placeholder="请输入类型ID"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目" prop="problemTitle">
        <el-input
            v-model="queryParams.problemTitle"
            placeholder="请输入题目"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="答题类型" prop="answerType">
        <el-select v-model="queryParams.answerType" placeholder="请选择答题类型" clearable>
          <el-option
              v-for="dict in train_question_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="难度" prop="problemDifficulty">
        <el-select v-model="queryParams.problemDifficulty" placeholder="请选择难度" clearable>
          <el-option
              v-for="dict in question_train_difficulty"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['member:memberTrainProblem:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['member:memberTrainProblem:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="memberTrainProblemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="所属会员" align="center" width="140" show-overflow-tooltip prop="memberName"/>
      <el-table-column label="类型" align="center" min-width="160" prop="typeId">
        <template #default="scope">
          {{ getTypeById(scope.row.typeId) }}
        </template>
      </el-table-column>
      <el-table-column label="题目" align="center" width="180" show-overflow-tooltip prop="problemTitle"/>
      <el-table-column label="答题类型" align="center" min-width="120" prop="answerType">
        <template #default="scope">
          <dict-tag :options="train_question_type" :value="scope.row.answerType"/>
        </template>
      </el-table-column>
      <el-table-column label="答案1" align="center" width="180" show-overflow-tooltip prop="answerOne"/>
      <el-table-column label="答案2" align="center" width="180" show-overflow-tooltip prop="answerTwo"/>
      <el-table-column label="答案3" align="center" width="180" show-overflow-tooltip prop="answerThree"/>
      <el-table-column label="答案4" align="center" width="180" show-overflow-tooltip prop="answerFour"/>
      <el-table-column label="答案5" align="center" width="180" show-overflow-tooltip prop="answerFive"/>
      <el-table-column label="真实答案" align="center" width="180" show-overflow-tooltip prop="realAnswer"/>
      <el-table-column label="问题分数" align="center" width="120" prop="problemScore"/>
      <el-table-column label="难度" align="center" width="120" prop="problemDifficulty">
        <template #default="scope">
          <dict-tag :options="question_train_difficulty" :value="scope.row.problemDifficulty"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="140" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['member:memberTrainProblem:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改会员错题本对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="memberTrainProblemRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属会员" prop="memberId">
          <el-input v-model="form.memberId" placeholder="请输入所属会员"/>
        </el-form-item>
        <el-form-item label="问题ID" prop="problemId">
          <el-input v-model="form.problemId" placeholder="请输入问题ID"/>
        </el-form-item>
        <el-form-item label="类型ID" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入类型ID"/>
        </el-form-item>
        <el-form-item label="题目" prop="problemTitle">
          <el-input v-model="form.problemTitle" placeholder="请输入题目"/>
        </el-form-item>
        <el-form-item label="答题类型 0 单选 1 多选 2 填空" prop="answerType">
          <el-select v-model="form.answerType" placeholder="请选择答题类型 0 单选 1 多选 2 填空">
            <el-option
                v-for="dict in train_question_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="答案1" prop="answerOne">
          <el-input v-model="form.answerOne" placeholder="请输入答案1"/>
        </el-form-item>
        <el-form-item label="答案2" prop="answerTwo">
          <el-input v-model="form.answerTwo" placeholder="请输入答案2"/>
        </el-form-item>
        <el-form-item label="答案3" prop="answerThree">
          <el-input v-model="form.answerThree" placeholder="请输入答案3"/>
        </el-form-item>
        <el-form-item label="答案4" prop="answerFour">
          <el-input v-model="form.answerFour" placeholder="请输入答案4"/>
        </el-form-item>
        <el-form-item label="答案5" prop="answerFive">
          <el-input v-model="form.answerFive" placeholder="请输入答案5"/>
        </el-form-item>
        <el-form-item label="真实答案" prop="realAnswer">
          <el-input v-model="form.realAnswer" placeholder="请输入真实答案"/>
        </el-form-item>
        <el-form-item label="问题分数" prop="problemScore">
          <el-input v-model="form.problemScore" placeholder="请输入问题分数"/>
        </el-form-item>
        <el-form-item label="难度 0 简单 1 中等 2 困难" prop="problemDifficulty">
          <el-select v-model="form.problemDifficulty" placeholder="请选择难度 0 简单 1 中等 2 困难">
            <el-option
                v-for="dict in question_train_difficulty"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MemberTrainProblem">
import {
  listMemberTrainProblem,
  getMemberTrainProblem,
  delMemberTrainProblem,
  addMemberTrainProblem,
  updateMemberTrainProblem
} from "@/api/member/memberTrainProblem";
import {listTrainProblemTypeAll} from "@/api/question/trainProblemType";

const {proxy} = getCurrentInstance();
const {
  train_question_type,
  question_train_difficulty
} = proxy.useDict('train_question_type', 'question_train_difficulty');

const memberTrainProblemList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    memberId: null,
    problemId: null,
    typeId: null,
    problemTitle: null,
    answerType: null,
    answerOne: null,
    answerTwo: null,
    answerThree: null,
    answerFour: null,
    answerFive: null,
    realAnswer: null,
    problemScore: null,
    problemDifficulty: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);
// 题目类型列表
const typeList = ref([]);


/** 查询会员错题本列表 */
function getList() {
  loading.value = true;
  listMemberTrainProblem(queryParams.value).then(response => {
    memberTrainProblemList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    memberId: null,
    problemId: null,
    typeId: null,
    problemTitle: null,
    answerType: null,
    answerOne: null,
    answerTwo: null,
    answerThree: null,
    answerFour: null,
    answerFive: null,
    realAnswer: null,
    problemScore: null,
    problemDifficulty: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null
  };
  proxy.resetForm("memberTrainProblemRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会员错题本";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getMemberTrainProblem(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会员错题本";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["memberTrainProblemRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateMemberTrainProblem(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMemberTrainProblem(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会员错题本编号为"' + _ids + '"的数据项？').then(function () {
    return delMemberTrainProblem(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('member/memberTrainProblem/export', {
    ...queryParams.value
  }, `memberTrainProblem_${new Date().getTime()}.xlsx`)
}

function getTypeList() {
  listTrainProblemTypeAll().then(res => {
    if (res.code === 200) {
      typeList.value = res.data;
    }
  })
}

function getTypeById(id) {
  if (typeList.value && typeList.value.length > 0) {
    const matArr = typeList.value.filter(x => x.id === id);
    if (matArr && matArr.length > 0) {
      return matArr[0].typeName
    }
  }
  return '-'
}

getList();
getTypeList();
</script>
